<template>
  <div class="login-pages">
    <div class="main_content">
      <img src="https://img01.yzcdn.cn/vant/cat.jpeg" alt="" class="logo" />
      <van-cell-group>
        <van-field
          v-model="info.username"
          maxlength="11"
          placeholder="用户名"
          class="has-bottom-border"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="info.password"
          :type="passFlag ? 'text' : 'password'"
          placeholder="密码"
          ref="passInput"
          class="has-bottom-border"
          :rules="[{ required: true, message: '请填写密码' }]"
        >
          <template #button>
            <van-icon
              name="eye"
              class="password-icon"
              @click="showPass"
              :class="{ 'is-show': passFlag }"
            />
          </template>
        </van-field>
      </van-cell-group>
      <van-button
        type="info"
        style="width: 80px; height: 80px; margin-top: 25px; border-radius: 100%"
        @click="toLogin"
      >
        →
      </van-button>
      <div class="flex-between bottom-link">
        <span @click="navTo('/update_pass')">忘记密码</span>
        <span @click="navTo('/register')">去注册</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLogin: false,
      passFlag: false,
      info: {
        username: '',
        password: ''
      }
    }
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    //登录
    toLogin() {
      // console.log('login')
      if (this.info.username === '' || this.info.password === '') {
        this.$toast.fail('用户名或者密码不可以为空')
        return
      }
      this.$api.serveapi
        .login(this.info)
        .then(res => {
          // console.log(res.data)
          if (res.data.status === 1100) {
            // 弹出错误提示
            this.$toast.fail('用户名或密码错误')
            this.info.password = ''
          } else if (res.data.status === 1105) {
            // 登录成功
            // 1.第一次登录，跳转到重置密码界面
            // 2.跳转到主页面，存储token
            localStorage.setItem('Authorization', res.data.token)
            localStorage.setItem('Username', this.info.username)
            localStorage.setItem('AvatarUrl', res.data.url)
            this.$router.replace('/home')
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    navTo(path) {
      if (path) {
        this.$router.push(path)
      }
    },
    //密码显隐
    showPass() {
      this.$refs.passInput.focus()
      this.passFlag = !this.passFlag
    }
  }
}
</script>

<style lang="scss" scoped>
.login-pages {
  padding: 10px;
  width: 100%;
  height: 100%;

  .van-cell {
    height: 60px;
    padding: 0 14px;
  }

  .van-field__body {
    height: 60px;
    padding: 0;

    input {
      height: 100%;
    }
  }

  .van-cell-group {
    .van-field {
      background-color: #f7f7f7;
      line-height: 60px;
      margin-top: 10px;
      font-size: 16px;
    }
  }

  [class*='van-hairline']::after {
    border: none;
  }

  .van-cell::after {
    border: none;
  }

  .has-bottom-border {
    // border-bottom: 1px solid var(--borderColor);
    border-radius: 30px;
  }

  .main_content {
    background: #fff;
    padding: 80px 35px 50px;
    border-radius: 20px;
    height: 100%;
    position: relative;
    .logo {
      width: 130px;
      height: 130px;
      display: block;
      margin: 0 auto 32px;
      border-radius: 100%;
    }
    .van-button {
      top: 6%;
      font-size: 30px;
      background: -webkit-linear-gradient(
        to right,
        #4776e6,
        #8e54e9
      ); /* Chrome 10-25, Safari 5.1-6 */
      background: linear-gradient(
        to right,
        #4776e6,
        #8e54e9
      ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    }
  }

  .bottom-link {
    padding: 80px 0;
    margin: 10px 30px;
    font-size: 14px;
    font-weight: 400;
    color: #999;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
  }

  .password-icon {
    font-size: 23px;
    color: #999;
    line-height: 70px;
  }
}
</style>
